<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;

class Collect extends Controller
{
    /**
     * 显示收藏的房屋信息
     *
     * @return \think\Response
     */
    public function index(){
        //从数据库导出导航条数据
        $list = Db::name('index_navigation')->select();
        //从数据库里查询友情链接
        $data = Db::table('lt_link')->select();
        //获取用户的session数据
       if(Session::get('login_admin')){$session = Session::get('login_admin');} else{$session = Session::get('login_tel');}
        $id=$session['id'];
        //从数据库里查询用户表数据
        $result = Db::table('lt_index_login')->where('id',$id)->select();

        //查询收藏结果
        $info = Db::name('index_collect')->where('lid',$id)->field('hid')->select();
        //dump($info);die;
         $house = array();
        foreach($info as $val){
            $house[] = Db::name('house_details')->where('id',$val['hid'])->find();
        }
        //dump($house);
        return view('collect/index',[
            'data'=>$data,
            'info'=>$list,
            'result'=>$result,
            'house'=>$house
        ]);
    }
    /**
     * 收藏房屋信息
     *
     * @return \think\Response
     */
    public function add($id)
    {
        //获取用户id和房屋信息id
        if(Session::get('login_admin')){$lid = session('login_admin')['id'];} else{$lid = session('login_tel')['id'];}

        if(!$lid){
            return 3;
        }
        $hid = $id;
        $data = [
            "lid"=>$lid,
            "hid"=>$hid
        ];
        //判断是否已被收藏
        $info = Db::name('index_collect')->where('lid',$lid)->where('hid',$hid)->find();
        if($info){
            return 2;
        } else {
            //插入数据库
            $result = Db::name('index_collect')->insert($data);
            return $result;
        }

    }

    /**
     * 取消收藏
     *
     * @return \think\Response
     */
    public function del($id)
    {
        //获取用户id和房屋信息id
        if(Session::get('login_admin')){$lid = session('login_admin')['id'];} else{$lid = session('login_tel')['id'];}
        $hid = $id;
        $result = Db::name('index_collect')->where('lid',$lid)->where('hid',$hid)->delete();
        return json($result);
    }
}
